<template>
  <div>
    <el-dialog
      :title="$t('CustomerManagementLang.configuration_tab')"
      :visible.sync="Visible"
      width="500px"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <div class="midContant clearfix">
        <span style="text-align: right" class="Span borderR"
          >{{$t('CustomerManagementLang.price_tag')}}:</span
        >
        <span class="Span  contant">
          <el-select
            ref="tag_ids"
            filterable
            @blur="handleBlur('tag_ids', $event)"
            @change="handleChang('tag_ids')"
            v-model="data.tag_ids"
            :placeholder="$t('common.select_plactholder')"
          >
            <el-option
              v-for="item in options_tag"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirm" size="small"
          >{{$t('common.confirm')}}</el-button
        >
        <el-button @click="cancel" size="small">{{$t('common.cancel')}}</el-button>
      </span>
      
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    // 接收绑定参数
    
  },
  data() {
    return {
      Visible: false,
      data: {
        tag_ids: "",
      },
      uid:'',
      options_tag:[],
      confirmTrue: true, //确定按钮保护
    };
  },
  created() {
   
  },
  mounted() {},
  watch: {},
  methods: {
    init(data) {
      this.getOptions()
      this.Visible = true;
      this.uid = data.uid;
      this.data.tag_ids = data.tag_ids;
    },
    //获取下拉单数据
    getOptions(){
      this.$get("memberTagList",{limit:-1}).then((res) => {
        this.options_tag = res.data;
        this.options_tag.unshift({id:0,name:this.$t('common.not_have')})
      });
    },
    // 提交
    confirm() {
      //是否有空值必选项标识
      let type = true;
      //输入框判断是否有值
      if (!this.data.tag_ids&&this.data.tag_ids!==0) {
        this.$refs.tag_ids.$el.childNodes[0].childNodes[1].childNodes[1].style.border ="1px solid red";
        type = false;
      }
      if (type && this.confirmTrue) {
          this.confirmTrue=false
          window.setTimeout(()=>{
            this.confirmTrue=true
          },1000);
          let params = new FormData();
          params.append("tag_ids", this.data.tag_ids);
          params.append("uid", this.uid);
          this.$postForm("updateMemberTagBinding", params).then((res) => {
            this.confirmTrue = true;
            if(res.success){
              this.$message.success(res.msg);
              this.$emit("success");
              this.cancel()
            }
          });
        }
    },
    //取消
    cancel() {
      this.data = {
        tag_ids: "",
      };
      this.Visible = false;
      
      let dom = document.getElementsByClassName("el-input__inner");
      for (let i = 0; i < dom.length; i++) {
         dom[i].style.border = "1px solid #E0E3E9";
      }
    },
    //输入框、下拉框失去焦点边框变色
    handleBlur(name, event) {
      if (!this.data[name]&&this.data[name]!==0) {
        event.target.style.border = "1px solid red";
      }
    },
    //下拉框有值后边框变色
    handleChang(data) {
      if (this.data[data].length==0) {
        this.$refs[data].$el.childNodes[1].childNodes[1].style.border = "1px solid red";
      }else{
        this.$refs[data].$el.childNodes[1].childNodes[1].style.border ="1px solid #E0E3E9 ";
      }
    },
  },
};
</script>

<style scoped lang="less" >
.Span{
    width: 50%;
  }
</style>